package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.PlatformCategory;
import com.addplus.townmall.server.api.model.town.expand.*;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品类目 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface PlatformCategoryMapper extends BaseAddMapper<PlatformCategory> {


    @Select("SELECT id as id ,name as name,icon as icon,parent_id as parentId,dept as dept,is_root as isRoot,path as path FROM platform_category WHERE FIND_IN_SET(#{platformId},path) and is_deleted=0 and is_online=1 ORDER BY dept,parent_id,sort DESC")
    List<ShopCategoryParamExpand> getPlatformCategoryChilderByParentId(@Param("platformId") Integer platformId);

    @Select("SELECT id as id ,name as name,icon as icon,parent_id as parentId,dept as dept,is_root as isRoot,path as path FROM platform_category WHERE is_deleted=0 and is_online=1 ORDER BY dept,parent_id,sort DESC")
    List<ShopCategoryParamExpand> getAllPlatformCategory();

    @Select("SELECT c.id FROM platform_category c WHERE FIND_IN_SET(#{platformId},c.path)")
    List<Integer> getPlatformCategoryByList(@Param("platformId") Integer paltformId);

    @Select("SELECT id as id ,name as name,icon as icon,parent_id as parentId, pay_cash_deposit as payCashDeposit, enable_noreason_return as enableNoreasonReturn, icon as icon, dept as dept,is_root as isRoot,path as path, gmt_modified as gmtModified, description as description FROM platform_category WHERE is_deleted=0 and is_online=1 ORDER BY dept DESC")
    List<PlatformCategoryExpand> getIPagePlatformCategory();

    @Select("SELECT id as id ,name as name,icon as icon,parent_id as parentId, pay_cash_deposit as payCashDeposit, enable_noreason_return as enableNoreasonReturn, icon as icon, dept as dept,is_root as isRoot,path as path, description as description ,gmt_modified as gmtModified FROM platform_category WHERE parent_id=0 AND is_deleted=0 and is_online=1 ORDER BY dept,parent_id,sort DESC")
    List<PlatformCategoryExpand> getPlatformCategory();

    @Select("SELECT id as id ,name as name,icon as icon,parent_id as parentId, pay_cash_deposit as payCashDeposit, enable_noreason_return as enableNoreasonReturn, icon as icon, dept as dept,is_root as isRoot,path as path, description as description ,gmt_modified as gmtModified FROM platform_category WHERE name=#{name} AND is_deleted=0 and is_online=1 ORDER BY dept,parent_id,sort DESC")
    List<PlatformCategoryExpand> getLikePlatformCategory(@Param("name") String name);

    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " name,\n" +
            " icon,\n" +
            " dept,\n" +
            " is_root,\n" +
            " description,\n" +
            " sort,\n" +
            " path,\n" +
            " enable_noreason_return,\n" +
            " pay_cash_deposit,\n" +
            " is_deleted,\n" +
            " gmt_modified,\n" +
            " parent_id \n" +
            "FROM\n" +
            " platform_category \n" +
            "WHERE\n" +
            " platform_category.is_deleted = 0 \n" +
            " AND path in \n" +
            " <foreach collection='platformDategory' open='('  item='item'  separator=',' close=')'> #{item}</foreach> " +
            " ORDER BY dept,sort DESC\n" +
            "</script>")
    List<PlatformCategoryExpand> getPlatformCategoryChilderByplatformCategoryId(@Param("platformDategory") List<Integer> platformDategoryIdList);

    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " name,\n" +
            " icon,\n" +
            " dept,\n" +
            " is_root,\n" +
            " description,\n" +
            " sort,\n" +
            " path,\n" +
            " enable_noreason_return,\n" +
            " pay_cash_deposit,\n" +
            " is_deleted,\n" +
            " gmt_modified,\n" +
            " parent_id \n" +
            "FROM\n" +
            " platform_category \n" +
            "WHERE\n" +
            " platform_category.is_deleted = 0 \n" +
            " AND path in \n" +
            " <foreach collection='platformDategory' open='('  item='item'  separator=',' close=')'> #{item}</foreach> " +
            " ORDER BY dept,sort DESC\n" +
            "</script>")
    List<PlatformCategoryWebExpand> getPlatformCategoryChildenByplatformCategoryId(@Param("platformDategory") List<Integer> platformDategoryIdList);

    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " name,\n" +
            " icon,\n" +
            " dept,\n" +
            " is_root,\n" +
            " description,\n" +
            " sort,\n" +
            " path,\n" +
            " enable_noreason_return,\n" +
            " pay_cash_deposit,\n" +
            " is_deleted,\n" +
            " gmt_modified,\n" +
            " parent_id \n" +
            "FROM\n" +
            " platform_category \n" +
            "WHERE\n" +
            " platform_category.is_deleted = 0 \n" +
            " AND path like concat('%',#{path},'%') \n" +
            " ORDER BY dept,sort desc\n" +
            "</script>")
    List<PlatformCategoryExpand> getPlatformCategoryChilderByPath(@Param("path") String path);


    @Select("SELECT\n" +
            " id \n" +
            "FROM\n" +
            " platform_category c \n" +
            "WHERE\n" +
            " FIND_IN_SET( #{categoryId}, path ) \n" +
            " AND dept != 1 \n" +
            " AND is_deleted = 0")
    List<Integer> getPlateformCategoryAllThirdDept(@Param("categoryId") Integer categoryId);


    @Select("<script>\n" +
            "SELECT\n" +
            "\tp.id,\n" +
            "\tp.shop_seal_img,\n" +
            "\tp.shop_name,\n" +
            "\tp.line_price,\n" +
            "\tp.sale_price,\n" +
            "\ts.id AS store_id,\n" +
            "\ts.store_name,\n" +
            "\td.shop_goods,\n" +
            "\t(IFNULL(p.sale_count, 0) + IFNULL(p.sale_virtual_count, 0)) AS sale_count,\n" +
            "\td.shop_comment_count,\n" +
            "\tsku.id AS shop_sku_id,\n" +
            "\tsku.sku_describe AS sku_value\n" +
            "FROM\n" +
            "\tshop_product p\n" +
            "\tLEFT JOIN store s ON s.id = p.store_id\n" +
            "\tAND s.store_status = 1\n" +
            "\tAND s.is_delete = 0\n" +
            "\tLEFT JOIN shop_product_describe d ON d.shop_product_id = p.id\n" +
            "\tAND d.is_deleted = 0\n" +
            "\tLEFT JOIN shop_specification_sku sku ON sku.shop_product_id = p.id AND sku.is_default = 1\n" +
            "WHERE\n" +
            "\tp.is_deleted = 0\n" +
            "\tAND p.online_status = 1\n" +
            "<if test=\"categoryListId != null\">" +
            "\tAND p.shop_category_id IN\n" +
            "    <foreach collection=\"categoryListId\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">\n" +
            "        #{item}\n" +
            "    </foreach>\n" +
            "</if>" +
            "<if test=\"overallRateSort != null or saleSort != null or serviceRateSort != null or priceSort != null \">\n" +
            "ORDER BY\n" +
            "</if> \n" +
            "    <if test=\"overallRateSort != null \">\n" +
            "        d.shop_product_overall_rate \n" +
            "        <if test=\"overallRateSort == 0\">\n" +
            "            ASC \n" +
            "        </if>\n" +
            "        <if test=\"overallRateSort == 1\">\n" +
            "            DESC \n" +
            "        </if>\n" +
            "        <if test=\"saleSort != null or serviceRateSort != null or priceSort != null \">\n" +
            "            ,\n" +
            "        </if>\n" +
            "    </if>\n" +
            "    <if test=\"saleSort != null \">\n" +
            "        (IFNULL(p.sale_count, 0) + IFNULL(p.sale_virtual_count, 0)) \n" +
            "        <if test=\"saleSort == 0\">\n" +
            "            ASC\n" +
            "        </if>\n" +
            "        <if test=\"saleSort == 1\">\n" +
            "            DESC\n" +
            "        </if>\n" +
            "        <if test=\"serviceRateSort != null or priceSort != null \">\n" +
            "            ,\n" +
            "        </if>\n" +
            "    </if>\n" +
            "    <if test=\"serviceRateSort != null \">\n" +
            "        d.shop_product_service_rate \n" +
            "        <if test=\"serviceRateSort == 0\">\n" +
            "            ASC\n" +
            "        </if>\n" +
            "        <if test=\"serviceRateSort == 1\">\n" +
            "            DESC\n" +
            "        </if>\n" +
            "        <if test=\"priceSort != null \">\n" +
            "            ,\n" +
            "        </if>\n" +
            "    </if>\n" +
            "    <if test=\"priceSort != null \">\n" +
            "         p.sale_price \n" +
            "        <if test=\"priceSort == 0\">\n" +
            "            ASC\n" +
            "        </if>\n" +
            "        <if test=\"priceSort == 1\">\n" +
            "            DESC\n" +
            "        </if>\n" +
            "    </if>\n" +
            "</script>")
    List<ShopProductExpand> getPlatformCatogoryAllShopProductExpand(Page page, @Param("categoryListId") List<Integer> categoryList,
                                                                    @Param("overallRateSort") Integer overallRateSort,
                                                                    @Param("saleSort") Integer saleSort,
                                                                    @Param("serviceRateSort") Integer serviceRateSort,
                                                                    @Param("priceSort") Integer priceSort);

    @Select("SELECT DISTINCT\n" +
            "pa.id,\n" +
            "pa.NAME,\n" +
            "dpst.personal_shop_id IS NOT NULL AS checked\n" +
            "FROM\n" +
            "platform_category pa\n" +
            "LEFT JOIN distribution_personal_shop_type dpst ON pa.id = dpst.shop_type_id\n" +
            "AND dpst.personal_shop_id = #{storeId}\n" +
            "AND dpst.is_delete = 0\n" +
            "WHERE\n" +
            "dept = 1\n" +
            "AND pa.is_deleted = 0 ")
    List<PlatformCategoryWebExpand> getPlatformCategoryFirstLevel(Page page, @Param("storeId") Integer storeId);

    @Select("<script>" +
            "SELECT\n" +
            " p.id,\n" +
            " c.path \n" +
            "FROM\n" +
            " platform_category c\n" +
            " INNER JOIN shop_product p ON p.shop_category_id = c.id \n" +
            "WHERE\n" +
            " p.id IN " +
            "<foreach collection='shopProductList' open='('  item='item'  separator=',' close=')'> #{item}</foreach>" +
            "</script>")
    List<PlatformCategory> getPlatformCategoryByShopProduct(@Param("shopProductList") List<Integer> shopProductList);

    @Select("SELECT * " +
            "FROM platform_category pc WHERE pc.is_deleted=0 AND pc.is_online=1")
    List<PlatformCategoryExpand> getAllCategoryList() throws Exception;

}
